/// 天气实体模型
class WeatherEntity {
  /// 天气唯一标识符
  final String uniqueIdentifier;

  /// 最高温度
  final int maxTemperature;

  /// 最低温度
  final int minTemperature;

  /// 天气描述
  final String weatherDescription;

  /// 天气图片路径
  final String weatherImagePath;

  const WeatherEntity({
    required this.uniqueIdentifier,
    required this.maxTemperature,
    required this.minTemperature,
    required this.weatherDescription,
    required this.weatherImagePath,
  });

  /// 创建空天气实例
  factory WeatherEntity.createEmptyInstance() {
    return const WeatherEntity(
      uniqueIdentifier: '',
      maxTemperature: 0,
      minTemperature: 0,
      weatherDescription: '',
      weatherImagePath: '',
    );
  }

  /// 从Map数据创建天气实例
  factory WeatherEntity.fromDataMap(Map<String, dynamic> dataMap) {
    return WeatherEntity(
      uniqueIdentifier: dataMap['uniqueIdentifier']?.toString() ?? '',
      maxTemperature: dataMap['maxTemperature'] ?? 0,
      minTemperature: dataMap['minTemperature'] ?? 0,
      weatherDescription: dataMap['weatherDescription']?.toString() ?? '',
      weatherImagePath: dataMap['weatherImagePath']?.toString() ?? '',
    );
  }

  /// 转换为Map数据
  Map<String, dynamic> convertToDataMap() {
    return {
      'uniqueIdentifier': uniqueIdentifier,
      'maxTemperature': maxTemperature,
      'minTemperature': minTemperature,
      'weatherDescription': weatherDescription,
      'weatherImagePath': weatherImagePath,
    };
  }

  /// 检查实例是否为空
  bool validateEmptyState() {
    return uniqueIdentifier.isEmpty || weatherDescription.isEmpty;
  }

  /// 获取温度范围字符串
  String getTemperatureRange() {
    return '$minTemperature°C - $maxTemperature°C';
  }

  @override
  String toString() {
    return 'WeatherEntity{id: $uniqueIdentifier, temp: ${getTemperatureRange()}, desc: $weatherDescription}';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is WeatherEntity &&
        other.uniqueIdentifier == uniqueIdentifier;
  }

  @override
  int get hashCode {
    return uniqueIdentifier.hashCode;
  }

  /// 预定义的天气数据集合
  static final List<WeatherEntity> _predefinedWeatherCollection = [
    WeatherEntity(
      uniqueIdentifier: 'weather_sunny',
      maxTemperature: 28,
      minTemperature: 18,
      weatherDescription: 'Sunny',
      weatherImagePath: 'assets/images/weather/sunny.png',
    ),
    WeatherEntity(
      uniqueIdentifier: 'weather_cloudy',
      maxTemperature: 22,
      minTemperature: 15,
      weatherDescription: 'Cloudy',
      weatherImagePath: 'assets/images/weather/cloudy.png',
    ),
    WeatherEntity(
      uniqueIdentifier: 'weather_rainy',
      maxTemperature: 20,
      minTemperature: 12,
      weatherDescription: 'Rainy',
      weatherImagePath: 'assets/images/weather/rainy.png',
    ),
    WeatherEntity(
      uniqueIdentifier: 'weather_thunderstorm',
      maxTemperature: 25,
      minTemperature: 16,
      weatherDescription: 'Thunderstorm',
      weatherImagePath: 'assets/images/weather/thunderstorm.png',
    ),
    WeatherEntity(
      uniqueIdentifier: 'weather_snowy',
      maxTemperature: 2,
      minTemperature: -5,
      weatherDescription: 'Snowy',
      weatherImagePath: 'assets/images/weather/snowy.png',
    ),
  ];

  /// 根据唯一标识符查找预定义的天气数据
  static WeatherEntity? findWeatherByIdentifier(String targetId) {
    try {
      return _predefinedWeatherCollection.firstWhere(
        (weather) => weather.uniqueIdentifier == targetId,
      );
    } catch (e) {
      return null;
    }
  }

  /// 获取所有预定义的天气数据
  static List<WeatherEntity> getAllPredefinedWeather() {
    return List.unmodifiable(_predefinedWeatherCollection);
  }

  /// 获取预定义数据的总数
  static int getPredefinedWeatherCount() {
    return _predefinedWeatherCollection.length;
  }

  /// 检查指定ID是否存在于预定义数据中
  static bool validateWeatherIdentifierExists(String targetId) {
    return _predefinedWeatherCollection
        .any((weather) => weather.uniqueIdentifier == targetId);
  }

  /// 随机获取一种天气数据
  static WeatherEntity getRandomWeather() {
    final random = DateTime.now().millisecondsSinceEpoch % _predefinedWeatherCollection.length;
    return _predefinedWeatherCollection[random];
  }
}
